Regex.Split 方法 (System.Text.RegularExpressions)

您所在的位置:网站首页 sqlserver 使用正则表达式 Regex.Split 方法 (System.Text.RegularExpressions)

Regex.Split 方法 (System.Text.RegularExpressions)

#Regex.Split 方法 (System.Text.RegularExpressions) | 来源: 网络整理| 查看: 265

在由 Regex 构造函数中指定的正则表达式定义的位置,将输入字符串拆分为子字符串数组指定的最大次数。 从输入字符串的指定字符位置开始搜索正则表达式模式。

public: cli::array ^ Split(System::String ^ input, int count, int startat); public string[] Split (string input, int count, int startat); member this.Split : string * int * int -> string[] Public Function Split (input As String, count As Integer, startat As Integer) As String() 参数 input String

要拆分的字符串。

count Int32

可拆分的最大次数。

startat Int32

输入字符串中将开始搜索的字符位置。

返回 String[]

字符串数组。

例外 ArgumentNullException

input 为 null。

ArgumentOutOfRangeException

startat 小于零或大于 input 的长度。

RegexMatchTimeoutException

发生超时。 有关超时的详细信息,请参阅“备注”部分。

注解

方法 Regex.Split 类似于 String.Split 方法,不同之处在于, Regex.Split 在由正则表达式而不是一组字符确定的分隔符处拆分字符串。 参数 count 指定将字符串拆分到 input 的子字符串的最大数目;最后一个字符串包含字符串的未拆分余数。 count如果值为零,则提供尽可能多地拆分的默认行为。 参数 startat 定义开始搜索第一个分隔符的点, (这可用于跳过前导空格) 。

有关 的 startat更多详细信息,请参阅 的 Match(String, Int32)“备注”部分。

如果未从字符串中的 count+1 位置找到匹配项,则 方法将返回包含字符串的单 input 元素数组。 如果找到一个或多个匹配项,则返回的数组的第一个元素将包含字符串的第一部分(从第一个字符到匹配前的一个字符)。

如果多个匹配项彼此相邻,并且找到的匹配项数至少比 count少两个 ,则会在数组中插入一个空字符串。 同样,如果在 处 startat找到匹配项,即字符串中的第一个字符,则返回的数组的第一个元素是空字符串。 也就是说,在确定匹配的子字符串数是否等于 count时,对相邻匹配项生成的空字符串进行计数。 在以下示例中,正则表达式 \d+ 用于查找字符串中数字字符的第一个子字符串的起始位置,然后从该位置开始,将字符串拆分为最多三次。 由于正则表达式模式与输入字符串的开头匹配,因此返回的字符串数组由一个空字符串、一个由五个字符组成的字母字符串以及该字符串的其余部分组成,

using System; using System.Text.RegularExpressions; public class Example { public static void Main() { string pattern = @"\d+"; Regex rgx = new Regex(pattern); string input = "123ABCDE456FGHIJ789KLMNO012PQRST"; Match m = rgx.Match(input); if (m.Success) { int startAt = m.Index; string[] result = rgx.Split(input, 3, startAt); for (int ctr = 0; ctr < result.Length; ctr++) { Console.Write("'{0}'", result[ctr]); if (ctr < result.Length - 1) Console.Write(", "); } Console.WriteLine(); } } } // The example displays the following output: // '', 'ABCDE', 'FGHIJKL789MNOPQ012' Imports System.Text.RegularExpressions Module Example Public Sub Main() Dim pattern As String = "\d+" Dim rgx As New Regex(pattern) Dim input As String = "123ABCDE456FGHIJ789KLMNO012PQRST" Dim m As Match = rgx.Match(input) If m.Success Then Dim startAt As Integer = m.Index Dim result() As String = rgx.Split(input, 3, startAt) For ctr As Integer = 0 To result.Length - 1 Console.Write("'{0}'", result(ctr)) If ctr < result.Length - 1 Then Console.Write(", ") Next Console.WriteLine() End If End Sub End Module ' The example displays the following output: ' '', 'ABCDE', 'FGHIJKL789MNOPQ012'

如果在正则表达式中使用捕获括号,则任何捕获的文本都包含在拆分字符串数组中。 但是,任何包含捕获文本的数组元素都不会在确定匹配数是否达到 时进行 count计数。 例如,将字符串“apple-apricot-plum-pear-p石榴-菠萝-桃子”拆分为字符串中从字符 15 开始的最多四个子字符串,将生成一个由七个元素组成的数组,如以下代码所示。

using System; using System.Text.RegularExpressions; public class Example { public static void Main() { string pattern = "(-)"; string input = "apple-apricot-plum-pear-pomegranate-pineapple-peach"; // Split on hyphens from 15th character on Regex regex = new Regex(pattern); // Split on hyphens from 15th character on string[] substrings = regex.Split(input, 4, 15); foreach (string match in substrings) { Console.WriteLine("'{0}'", match); } } } // The method writes the following to the console: // 'apple-apricot-plum' // '-' // 'pear' // '-' // 'pomegranate' // '-' // 'pineapple-peach' Imports System.Text.RegularExpressions Module Example Public Sub Main() Dim pattern As String = "(-)" Dim input As String = "apple-apricot-plum-pear-pomegranate-pineapple-peach" Dim regex As Regex = New Regex(pattern) ' Split on hyphens from 15th character on Dim substrings() As String = regex.Split(input, 4, 15) For Each match As String In substrings Console.WriteLine("'{0}'", match) Next End Sub End Module ' The example displays the following output: ' 'apple-apricot-plum' ' '-' ' 'pear' ' '-' ' 'pomegranate' ' '-' ' 'pineapple-peach'

但是,当正则表达式模式包含多组捕获括号时,此方法的行为取决于.NET Framework的版本。 在 .NET Framework 1.0 和 1.1 中,如果在第一组捕获括号中找不到匹配项,则从其他捕获括号捕获的文本不包括在返回的数组中。 从 .NET Framework 2.0 开始,所有捕获的文本也会添加到返回的数组中。 例如,以下代码使用两组捕获括号来提取字符串中的单个单词。 第一组捕获括号捕获连字符,第二组捕获垂直条。 如果示例代码是在 .NET Framework 1.0 或 1.1 下编译并运行的,则它不包括垂直条形字符;如果它是在 .NET Framework 2.0 或更高版本下编译和运行的,则包含这些字符。

using System; using System.Text.RegularExpressions; public class Example { public static void Main() { string pattern = "(-)|([|])"; // possible delimiters found in string string input = "apple|apricot|plum|pear|pomegranate|pineapple|peach"; Regex regex = new Regex(pattern); // Split on delimiters from 15th character on string[] substrings = regex.Split(input, 4, 15); foreach (string match in substrings) { Console.WriteLine("'{0}'", match); } } } // In .NET 2.0 and later, the method returns an array of // 7 elements, as follows: // apple|apricot|plum' // '|' // 'pear' // '|' // 'pomegranate' // '|' // 'pineapple|peach' // In .NET 1.0 and 1.1, the method returns an array of // 4 elements, as follows: // 'apple|apricot|plum' // 'pear' // 'pomegranate' // 'pineapple|peach' Imports System.Text.RegularExpressions Module Example Public Sub Main() Dim pattern As String = "(-)|([|])" ' possible delimiters found in string Dim input As String = "apple|apricot|plum|pear|pomegranate|pineapple|peach" Dim regex As Regex = New Regex(pattern) ' Split on delimiters from 15th character on Dim substrings() As String = regex.Split(input, 4, 15) For Each match As String In substrings Console.WriteLine("'{0}'", match) Next End Sub End Module ' In .NET 2.0, the method returns an array of ' 7 elements, as follows: ' apple|apricot|plum' ' '|' ' 'pear' ' '|' ' 'pomegranate' ' '|' ' 'pineapple|peach' ' In .NET 1.0 and 1.1, the method returns an array of ' 4 elements, as follows: ' 'apple|apricot|plum' ' 'pear' ' 'pomegranate' ' 'pineapple|peach'

如果正则表达式可以匹配空字符串, Split 则将字符串拆分为单字符字符串数组,因为可以在每个位置找到空字符串分隔符。 以下示例将字符串“characters”拆分为输入字符串包含的任意数量的元素,从字符“a”开始。 由于 null 字符串与输入字符串的末尾匹配,因此在返回的数组的末尾插入一个 null 字符串。

using System; using System.Text.RegularExpressions; public class Example { public static void Main() { string input = "characters"; Regex regex = new Regex(""); string[] substrings = regex.Split(input, input.Length, input.IndexOf("a")); Console.Write("{"); for(int ctr = 0; ctr < substrings.Length; ctr++) { Console.Write(substrings[ctr]); if (ctr < substrings.Length - 1) Console.Write(", "); } Console.WriteLine("}"); } } // The example displays the following output: // {, c, h, a, r, a, c, t, e, rs} Imports System.Text.RegularExpressions Module Example Public Sub Main() Dim input As String = "characters" Dim regex As New Regex("") Dim substrings() As String = regex.Split(input, input.Length, _ input.IndexOf("a")) Console.Write("{") For ctr As Integer = 0 to substrings.Length - 1 Console.Write(substrings(ctr)) If ctr < substrings.Length - 1 Then Console.Write(", ") Next Console.WriteLine("}") End Sub End Module ' The example produces the following output: ' {, c, h, a, r, a, c, t, e, rs}

RegexMatchTimeoutException如果拆分操作的执行时间超过构造函数指定的Regex.Regex(String, RegexOptions, TimeSpan)超时间隔,则会引发异常。 如果在调用构造函数时未设置超时间隔,当操作超过为创建对象的应用程序域 Regex 建立的任何超时值时,将引发异常。 如果在构造函数调用或应用程序域的属性中未定义 Regex 超时,或者超时值为 Regex.InfiniteMatchTimeout,则不会引发异常

另请参阅 正则表达式语言 - 快速参考 适用于


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3